# -*- coding: utf-8 -*-
# Created by shu2015626 on 2021/7/10

from typing import Optional

import uvicorn as uvicorn
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class CityInfo(BaseModel):
    province: str
    country: str
    is_affected: Optional[bool] = None  # 与bool的区别是可以不传，默认是null


# # 同步
# @app.get("/")
# def hello_world():
#     return {"hello": "world"}
#
#
# @app.get('/city/{city}')
# def result(city: str, query_string: Optional[str] = None):
#     # 路径参数：两个/之间的数据。需要取一个名字
#     # 查询参数：?后的数据，直接定义
#     return {'city': city, 'query_string': query_string}
#
#
# @app.put('/city/{city}')
# def result(city: str, city_info: CityInfo):
#     # 路径参数：两个/之间的数据。需要取一个名字
#     # 查询参数：?后的数据，直接定义
#     return {'city': city, 'country': city_info.country, 'is_affected': city_info.is_affected}


# 异步
@app.get("/")
async def hello_world():
    return {"hello": "world"}


@app.get('/city/{city}')
async def result(city: str, query_string: Optional[str] = None):
    # 路径参数：两个/之间的数据。需要取一个名字
    # 查询参数：?后的数据，直接定义
    return {'city': city, 'query_string': query_string}


@app.put('/city/{city}')
async def result(city: str, city_info: CityInfo):
    # 路径参数：两个/之间的数据。需要取一个名字
    # 查询参数：?后的数据，直接定义
    return {'city': city, 'country': city_info.country, 'is_affected': city_info.is_affected}


if __name__ == '__main__':
    # 启动命令：uvicorn tests.hello_world:app --reload
    uvicorn.run("tests.hello_world:app", host="127.0.0.1", port=5000, log_level="info", reload=True)
